package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.BrandDetailVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理品牌数据的控制器
 *
 * @author stephen
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/brands")
@Api(tags = "01.品牌管理模块")
public class BrandController {

    @Autowired
    private IBrandService brandService;


    public BrandController() {
        log.info("创建控制器：BrandController");
    }

    /**
     * 添加品牌数据
     * @param brandAddNewDTO 封装品牌数据
     * @return 返回添加结果
     */
    // http://localhost:9080/brands/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理【添加品牌】请求，参数：{}",brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 根据id删除品牌数据
     * @param id 品牌数据id
     * @return 返回删除结果
     */
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiImplicitParam(name = "id",value = "品牌id",dataType = "Long",required = true)
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    public String deleteById(@PathVariable Long id){
        log.debug("开始处理【删除品牌】请求，参数：id={}",id);
        brandService.deleteById(id);
        return "删除品牌成功";
    }


    /**
     * 查询品牌列表
     * @return 查询到的品牌数据列表
     */
    //http://localhost:9080/brands/list
    @GetMapping("/list")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 300)
    public JsonResult<List<BrandListItemVO>> list(){
        List list = brandService.listData();
        return JsonResult.ok(list);
    }


    @ApiOperation("品牌详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/detail")
    public JsonResult<BrandDetailVO> BrandDeatil(Long id){
        return JsonResult.ok(brandService.detailData(id));
    }
}
